package com.pet.serve.controller;

import com.pet.serve.entity.Collect;
import com.pet.serve.request.CollectRequest;
import com.pet.serve.response.ApiResponse;
import com.pet.serve.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/collect")
public class CollectController {

    private final CollectService collectService;

    @Autowired
    public CollectController(CollectService collectService) {
        this.collectService = collectService;
    }

    @GetMapping("/get")
    public ResponseEntity<ApiResponse> getCollectsByUsername(
            @RequestParam String username, @RequestParam(required = false) Collect.collectType type) {

        List<Collect> petCollects = new ArrayList<>();
        List<Collect> goodCollects = new ArrayList<>();

        // 获取宠物和商品收藏
        if (type != null) {
            if (type == Collect.collectType.pet) {
                petCollects = collectService.getCollectsByUsernameAndType(username, Collect.collectType.pet);
            } else if (type == Collect.collectType.good) {
                goodCollects = collectService.getCollectsByUsernameAndType(username, Collect.collectType.good);
            }
        } else {
            petCollects = collectService.getCollectsByUsernameAndType(username, Collect.collectType.pet);
            goodCollects = collectService.getCollectsByUsernameAndType(username, Collect.collectType.good);
        }

        // 合并宠物和商品到一个列表中
        List<Collect> combinedCollects = new ArrayList<>();
        combinedCollects.addAll(petCollects);
        combinedCollects.addAll(goodCollects);

        // 设置宠物的 goodid 为 null，商品的 petid 为 null
        for (Collect collect : combinedCollects) {
            if (collect.getPetid() != null) {
                collect.setGoodid(null);
            } else {
                collect.setPetid(null);
            }
        }

        // 计算总收藏品数量
        int total = combinedCollects.size();


        return ResponseEntity.ok(new ApiResponse(true, null, combinedCollects,total));
    }





    // 获取指定用户名和收藏类型的收藏
    @GetMapping("/byUsernameAndType")
    public ResponseEntity<List<Collect>> getCollectsByUsernameAndType(@RequestParam String username, @RequestParam Collect.collectType type) {
        List<Collect> collects = collectService.getCollectsByUsernameAndType(username, type);
        return ResponseEntity.ok(collects);
    }

    // 删除指定商品的收藏
    @DeleteMapping("/delete/good")
    public ResponseEntity<ApiResponse> deleteCollectByGoodId(@RequestParam String username, @RequestParam Long goodid) {
        collectService.deleteCollectByGoodId(username, goodid);
        return ResponseEntity.ok(new ApiResponse(true, "Collect deleted successfully"));
    }

    // 删除指定宠物的收藏
    @DeleteMapping("/delete/pet")
    public ResponseEntity<ApiResponse> deleteCollectByPetId(@RequestParam String username, @RequestParam Long petid) {
        collectService.deleteCollectByPetId(username, petid);
        return ResponseEntity.ok(new ApiResponse(true, "Collect deleted successfully"));
    }

    // 获取所有有效的收藏
    @GetMapping("/active")
    public ResponseEntity<List<Collect>> getActiveCollects(@RequestParam String username) {
        List<Collect> activeCollects = collectService.getActiveCollectsByUsername(username);
        return ResponseEntity.ok(activeCollects);
    }

    // 判断商品是否在收藏中
    @GetMapping("/isCollected/good")
    public ResponseEntity<Boolean> isGoodCollected(@RequestParam String username, @RequestParam Long goodid) {
        boolean isCollected = collectService.isGoodCollected(username, goodid);
        return ResponseEntity.ok(isCollected);
    }

    // 判断宠物是否在收藏中
    @GetMapping("/isCollected/pet")
    public ResponseEntity<Boolean> isPetCollected(@RequestParam String username, @RequestParam Long petid) {
        boolean isCollected = collectService.isPetCollected(username, petid);
        return ResponseEntity.ok(isCollected);
    }

    // 添加收藏
    @PostMapping("/add")
    public ResponseEntity<Map<String, Object>> addCollect(@RequestBody CollectRequest request) {
        Map<String, Object> response = new HashMap<>();
        String username = request.getUsername();
        Long petid = request.getPetid();
        Long goodid = request.getGoodid();
        String status = request.getStatus();
        String type = request.getType();

        // 确保 type 不为空
        if (type == null) {
            response.put("success", false);
            response.put("message", "Type cannot be null");
            return ResponseEntity.badRequest().body(response);
        }

        // 根据 type 决定插入到哪个表
        if ("PET".equalsIgnoreCase(type) && petid != null) {
            collectService.addPetCollect(username, petid, status); // 处理宠物的收藏
        } else if ("GOOD".equalsIgnoreCase(type) && goodid != null) {
            collectService.addGoodCollect(username, goodid, status); // 处理商品的收藏
        } else {
            response.put("success", false);
            response.put("message", "Invalid parameters");
            return ResponseEntity.badRequest().body(response);
        }

        // 操作成功时返回
        response.put("success", true);
        response.put("message", "Collect added successfully");
        return ResponseEntity.ok(response);
    }


}
